# -*- coding: utf-8 -*-
# standard
import os
import sys
import re

# third

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from functions import *


"""
@Title:   
@File: 统计爬虫机的下载速度.py
@Author: walle 2023年10日18日 16时13分10秒
@Version: 1.0.0
@Desc: 
给定时间，统计爬虫机器的下载速度，包含数量速度和大小速度
"""

BEGIN = datetime(2023, 12, 18, 16, 0, 0)
END = datetime(2023, 12, 19, 9, 0, 0)
BEGIN = datetime(2023, 12, 19, 12, 0, 0)  
BEGIN = datetime(2023, 12, 19, 16, 0, 0)
# END = datetime(2023, 12, 19, 15, 0, 0)
END = datetime.now()
# END = datetime.now()
K = 1024   # 1K 多少字节
G = 1024 ** 3  # 1G 多少字节


def summary_download_info():
    # 统计下载速度
    db_session = new_db_session()
    # 修复超时的数据
    # query = db_session.query(MediaFileOrm).filter(MediaFileOrm.download_begin != None, MediaFileOrm.error != "").order_by(MediaFileOrm.download_begin)
    # for x in query:
    #     if "timed out" in x.error:
    #         print(x.error, x.download_begin, x.download_end)
    #         x.error = ""
    #         x.download_begin = None
    # db_session.commit()
    # db_session.close()
    cond = and_(
        MediaFileOrm.download_begin != None,
        MediaFileOrm.download_end != None,
        MediaFileOrm.download_begin >= BEGIN,
        MediaFileOrm.download_end <= END
    )
    size, num = 0, 0  # 尺寸和数量
    first, last = None, None  # 第一个的开始时间和最后一个的结束时间
    query = db_session.query(MediaFileOrm).where(cond).order_by(MediaFileOrm.download_begin)
    first, last = None, None
    current_speeds = []  # 存放每个视频的下载速度的集合。
    for x in query:
        num += 1
        if first is None:
            first = x.download_begin
        if last is None:
            last = x.download_end
        elif x.download_begin <= last:
            pass
        else:
            last = x.download_end
        resource = x.resource
        video_info: dict = resource['video_info']
        video_info = [{"quality": int(re.search(r"(\d+)", k).group()), "size": v} for k, v in video_info.items()]
        video_info.sort(key=lambda x: x['quality'])
        if video_info[0]['size'] is None:
            continue
        size += int(video_info[0]['size'])
        d = (x.download_end - x.download_begin).total_seconds()
        current_speed = round(int(video_info[0]['size']) / K / d, 2) # 当前视频的速度, 单位 K/S
        current_speeds.append(current_speed)
    t = (last - first).total_seconds() / 3600
    speed_num = num / t
    speed_size = size / t / G
    avg_current_speed =  round(sum(current_speeds) / len(current_speeds), 2) # 有效的视频的下载速度的平均值，这个忽略了下载失败和文件之间的等待，更能反应网络的真实速度
    print(f"下载了{num}个文件，共{size / G: 0.2f}G, 用时{t: 0.2f}小时，下载速度平均值{avg_current_speed}K/S，总体平均速度{speed_num:0.2f}个/小时，折合平{speed_size:0.2f}G/小时, 折算成24小时约 {speed_num * 24 : 0.2f} 个视频")


if __name__ == '__main__':
    summary_download_info()
    pass